<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Introduction</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 9.1.2 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Indexes"
HREF="indexes.html"><LINK
REL="PREVIOUS"
TITLE="Indexes"
HREF="indexes.html"><LINK
REL="NEXT"
TITLE="Index Types"
HREF="indexes-types.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2011-12-01T22:07:59"></HEAD
><BODY
CLASS="SECT1"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PostgreSQL 9.1.2 Documentation</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="Indexes"
HREF="indexes.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="indexes.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
>Chapter 11. Indexes</TD
><TD
WIDTH="20%"
ALIGN="right"
VALIGN="top"
><A
TITLE="Index Types"
HREF="indexes-types.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="INDEXES-INTRO"
>11.1. Introduction</A
></H1
><P
>   Suppose we have a table similar to this:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE TABLE test1 (
    id integer,
    content varchar
);</PRE
><P>
   and the application issues many queries of the form:
</P><PRE
CLASS="PROGRAMLISTING"
>SELECT content FROM test1 WHERE id = <TT
CLASS="REPLACEABLE"
><I
>constant</I
></TT
>;</PRE
><P>
   With no advance preparation, the system would have to scan the entire
   <TT
CLASS="STRUCTNAME"
>test1</TT
> table, row by row, to find all
   matching entries.  If there are many rows in
   <TT
CLASS="STRUCTNAME"
>test1</TT
> and only a few rows (perhaps zero
   or one) that would be returned by such a query, this is clearly an
   inefficient method.  But if the system has been instructed to maintain an
   index on the <TT
CLASS="STRUCTFIELD"
>id</TT
> column, it can use a more
   efficient method for locating matching rows.  For instance, it
   might only have to walk a few levels deep into a search tree.
  </P
><P
>   A similar approach is used in most non-fiction books:  terms and
   concepts that are frequently looked up by readers are collected in
   an alphabetic index at the end of the book.  The interested reader
   can scan the index relatively quickly and flip to the appropriate
   page(s), rather than having to read the entire book to find the
   material of interest.  Just as it is the task of the author to
   anticipate the items that readers are likely to look up,
   it is the task of the database programmer to foresee which indexes
   will be useful.
  </P
><P
>   The following command can be used to create an index on the
   <TT
CLASS="STRUCTFIELD"
>id</TT
> column, as discussed:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE INDEX test1_id_index ON test1 (id);</PRE
><P>
   The name <TT
CLASS="STRUCTNAME"
>test1_id_index</TT
> can be chosen
   freely, but you should pick something that enables you to remember
   later what the index was for.
  </P
><P
>   To remove an index, use the <TT
CLASS="COMMAND"
>DROP INDEX</TT
> command.
   Indexes can be added to and removed from tables at any time.
  </P
><P
>   Once an index is created, no further intervention is required: the
   system will update the index when the table is modified, and it will
   use the index in queries when it thinks doing so would be more efficient
   than a sequential table scan.  But you might have to run the
   <TT
CLASS="COMMAND"
>ANALYZE</TT
> command regularly to update
   statistics to allow the query planner to make educated decisions.
   See <A
HREF="performance-tips.html"
>Chapter 14</A
> for information about
   how to find out whether an index is used and when and why the
   planner might choose <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>not</I
></SPAN
> to use an index.
  </P
><P
>   Indexes can also benefit <TT
CLASS="COMMAND"
>UPDATE</TT
> and
   <TT
CLASS="COMMAND"
>DELETE</TT
> commands with search conditions.
   Indexes can moreover be used in join searches.  Thus,
   an index defined on a column that is part of a join condition can
   also significantly speed up queries with joins.
  </P
><P
>   Creating an index on a large table can take a long time.  By default,
   <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> allows reads (<TT
CLASS="COMMAND"
>SELECT</TT
> statements) to occur
   on the table in parallel with index creation, but writes (<TT
CLASS="COMMAND"
>INSERT</TT
>,
   <TT
CLASS="COMMAND"
>UPDATE</TT
>, <TT
CLASS="COMMAND"
>DELETE</TT
>) are blocked until the index build is finished.
   In production environments this is often unacceptable.
   It is possible to allow writes to occur in parallel with index
   creation, but there are several caveats to be aware of &mdash;
   for more information see <A
HREF="sql-createindex.html#SQL-CREATEINDEX-CONCURRENTLY"
><I
>Building Indexes Concurrently</I
></A
>.
  </P
><P
>   After an index is created, the system has to keep it synchronized with the
   table.  This adds overhead to data manipulation operations.
   Therefore indexes that are seldom or never used in queries
   should be removed.
  </P
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="indexes.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="indexes-types.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Indexes</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="indexes.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Index Types</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>